package com.seelyn.ddvideo.spider;

import com.seelyn.ddvideo.event.ParseErrorEvent;
import com.seelyn.ddvideo.event.SystemEventPublisher;
import com.seelyn.ddvideo.model.Video;
import com.seelyn.ddvideo.model.VideoPlayer;
import com.seelyn.ddvideo.service.VideoService;
import org.jsoup.nodes.Element;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * @author slacrey
 * @since 2017/6/11
 */
public class YoukuVideoInfoCartoonProcessor implements PageProcessor {

    private SystemEventPublisher eventPublisher;
    private VideoService videoService;
    private Video video;
    private List<VideoPlayer> players;
    private String xpathString = "/html/body/div[@id='sMain']/div[@id='module_basic_playarea']/div/div[@id='player_sidebar']/div/div[@id='listareashow']/div[@id='module_basic_playlist']/div[2]/div[@id='Drama']/div[2]/div/div/div/div/div/div/div[1]/li";

    public YoukuVideoInfoCartoonProcessor(SystemEventPublisher eventPublisher, VideoService videoService, Video video) {
        this.eventPublisher = eventPublisher;
        this.videoService = videoService;
        this.video = video;
        this.players = video.getPlayers(FromSiteEnum.YOUKU);
    }

    private Site site = Site.me().setDomain("v.youku.com")
            .setCharset("UTF-8")
            .setSleepTime(100)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");


    @Override
    public void process(Page page) {

        VideoPlayer player = null;
        Html body = page.getHtml();
        Selectable spanItems = body.xpath(xpathString);
        if (spanItems == null || spanItems.all().isEmpty()) {
            eventPublisher.publishParseError(new ParseErrorEvent(video, FromSiteEnum.YOUKU, MovieTypeEnum.cartoon, PageTypeEnum.DETAIL));
            return;
        }

        //清空以前存在的播放地址
        this.players.clear();

        List<String> items = spanItems.all();
        Html itemHtml;
        int i = 0;
        for (String item : items) {

            itemHtml = Html.create(item);

            String name = "";
            Element aElement = itemHtml.getDocument().body().child(0);
            String link = aElement.attr("href");

            if (aElement.children().size() < 3) {
                continue;
            } else {
                Element spanElement = aElement.child(1);
                name = spanElement.text();
            }

            if (StringUtils.isEmpty(link) || StringUtils.isEmpty(name)) {
                continue;
            }
            player = new VideoPlayer();
            player.setName(name);
            player.setUrl(VideoUtils.YOUKU_PREFIX_URL + link);
            player.setOrder(i);
            i++;
            this.players.add(player);

        }

        videoService.saveVideo(video);

    }

    @Override
    public Site getSite() {
        return site;
    }

}
